const path = require('path');
const resolve = (dir) => path.join(__dirname, dir);
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
// let isDev = process.env.NODE_ENV;  //当前的环境
const CompressionPlugin = require('compression-webpack-plugin')

console.log(IS_PROD, process.env.VUE_APP_SRC);
module.exports = {

    publicPath: './',
    assetsDir: 'static',
    // assetsDir: 'assets',
    productionSourceMap: false,
    configureWebpack: config => {
        if (process.env.NODE_ENV === 'production') {
            return {
                plugins: [new CompressionPlugin({
                    test: /\.js$|\.html$|\.css/,
                    threshold: 10240,
                    deleteOriginalAssets: false
                })]
            }
        }
    },
    devServer: {
        open: IS_PROD, // 生产环境才使用浏览器自动打开
        https: false,
        port: 8100,
        proxy: {
            '/localApi1': {
                // 本地
                target: process.env.VUE_APP_SRC || 'http://127.0.0.1:8080',
                changeOrigin: process.env.NODE_ENV === 'development' ? true : false, // 开发环境才开启反向代理, 运行(生产)环境不开启
                pathRewrite: {
                    '^/localApi1': '/'
                }
            },
            '/localApi2': {
                // 本地
                target: process.env.VUE_APP_SRC || 'http://127.0.0.1:8080',
                changeOrigin: process.env.NODE_ENV === 'development' ? true : false, // 开发环境才开启反向代理, 运行(生产)环境不开启
                pathRewrite: {
                    '^/localApi2': '/'
                }
            },
            // 匹配到/localApi开头时 替换成target指定的地址
            '/localApi': {
                // 本地
                target: process.env.VUE_APP_SRC || 'http://127.0.0.1:8080',
                changeOrigin: process.env.NODE_ENV === 'development' ? true : false, // 开发环境才开启反向代理, 运行(生产)环境不开启
                pathRewrite: {
                    // 把匹配到的/localApi这段开头 替换成 /
                    '^/localApi': '/'
                }
            }
        }
    },
    chainWebpack: config => {
        // 添加别名
        config.resolve.alias
            .set('@', resolve('src'))
            .set('assets', resolve('src/assets'))
            .set('components', resolve('src/components'))
            .set('layout', resolve('src/layout'))
            .set('base', resolve('src/base'))
            .set('static', resolve('src/static'))
            .set('utils', resolve('src/utils'))
            .set('common', resolve('src/components/common'));
    }
}